package com.wyzz.hr.action;

import java.util.*;

import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IPositionBaseBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;

public class AssembleCurrent extends AssembleDeptList {

	public AssembleCurrent() {
	}

	protected void getDeptList() {
		IDepartmentBO departmentBo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		allResultList = departmentBo.FindEnabledDepartment();
		prepareInitialDeptList();
	}

	protected void setActualNum() {
		IDepartmentBO departmentBo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		List deptEmpNosList = departmentBo.getAllActiveDeptEmpNumList();
		label0: for (int i = 0; i < deptList.size(); i++) {
			Department dept = (Department) deptList.get(i);
			String deptId = dept.getId();
			int j = 0;
			do {
				if (j >= deptEmpNosList.size())
					continue label0;
				Object deptActualNum[] = (Object[]) (Object[]) deptEmpNosList
						.get(j);
				String turnId = (String) deptActualNum[1];
				if (deptId.equals(turnId)) {
					dept.setActualNum((Integer) deptActualNum[0]);
					continue label0;
				}
				j++;
			} while (true);
		}

	}

	protected void setOrderNum() {
		IPositionBaseBo positionBaseBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		List activePbList = positionBaseBo.findAllActivePb();
		Map deptPbMap = new HashMap();
		for (int i = 0; i < activePbList.size(); i++) {
			PositionBase pb = (PositionBase) activePbList.get(i);
			String deptKey = pb.getPbDeptId().getId();
			if (deptPbMap.containsKey(deptKey)) {
				List deptPbList = (List) deptPbMap.get(deptKey);
				deptPbList.add(pb);
			} else {
				List deptPbList = new ArrayList();
				deptPbList.add(pb);
				deptPbMap.put(deptKey, deptPbList);
			}
		}

		for (int i = 0; i < deptList.size(); i++) {
			Department dept = (Department) deptList.get(i);
			List deptPbList = (List) deptPbMap.get(dept.getId());
			int sumOrderNum = 0;
			if (deptPbList != null) {
				for (int j = 0; j < deptPbList.size(); j++) {
					PositionBase pb = (PositionBase) deptPbList.get(j);
					if (pb.getPbMaxEmployee() != null)
						sumOrderNum += pb.getPbMaxEmployee().intValue();
				}

			}
			dept.setOrderNum(Integer.valueOf(sumOrderNum));
		}

	}

	protected void setDeptHead() {
		IPositionBaseBo positionBaseBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		List respPbList = positionBaseBo.findAllRespPb();
		Map pbEmpListMap = getPbEmpListMap();
		Map deptEmpListMap = new HashMap();
		for (int i = 0; i < respPbList.size(); i++) {
			PositionBase pb = (PositionBase) respPbList.get(i);
			String deptKey = pb.getPbDeptId().getId();
			List transferEmpList = (List) pbEmpListMap.get(pb.getId());
			deptEmpListMap.put(deptKey, transferEmpList);
		}

		for (int i = 0; i < deptList.size(); i++) {
			Department dept = (Department) deptList.get(i);
			List deptHeadEmpList = (List) deptEmpListMap.get(dept.getId());
			if (deptHeadEmpList != null && deptHeadEmpList.size() == 1)
				dept.setDeptHead((Employee) deptHeadEmpList.get(0));
		}

	}

	private Map getPbEmpListMap() {
		IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory.getBean("empBo");
		List empList = empBo.loadAll();
		Map pbEmpListMap = new HashMap();
		for (int j = 0; j < empList.size(); j++) {
			Employee emp = (Employee) empList.get(j);
			if (emp.getEmpPbNo() == null)
				continue;
			String pbKey = emp.getEmpPbNo().getId();
			if (pbEmpListMap.containsKey(pbKey)) {
				List mapEmpList = (List) pbEmpListMap.get(pbKey);
				mapEmpList.add(emp);
			} else {
				List mapEmpList = new ArrayList();
				mapEmpList.add(emp);
				pbEmpListMap.put(pbKey, mapEmpList);
			}
		}

		return pbEmpListMap;
	}
}
